#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include "constants.h"
#include "HitDetection.h"
#include "SDL_mixer.h"

int GAME_LIFES = 3;

int GAME_STARTED = 0;
int SHOW_GAME_SCORE = 0;
int ACTIVATE_SOUND = 1;

int main ( int argc, char** argv )
{
    Mix_Music *music = NULL;

    SDL_Surface *screen;	//This pointer will reference the backbuffer
    SDL_Surface *image;//This pointer will reference our bitmap sprite
    SDL_Surface *menuSurface;
    SDL_Surface *gameOverSurface;
    SDL_Surface *gameHelpSurface;

    SDL_Surface *photo;
    SDL_Surface *fin;
    SDL_Surface *up;
    SDL_Surface *down;
    SDL_Surface *left;
    SDL_Surface *right;

    SDL_Surface *photo1;
    SDL_Surface *photo2;
    SDL_Surface *photo3;

    //SDL_Surface *temp;	//This pointer will temporarily reference our bitmap sprite
    //SDL_Rect src, dest;	//These rectangles will describe the source and destination regions of our blit
    SDL_Rect fille;
    SDL_Rect diplome;
    SDL_Rect cadre;
    SDL_Event event;
    SDL_Rect coeur1;
    SDL_Rect coeur2;
    SDL_Rect coeur3;

    int done=1;
    int direction=0; //0:right 1:left 2:up 3:down



    //We must first initialize the SDL video component, and check for success
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0) {
        fprintf(stderr, "Warning: Audio could not be setup for 11025 Hz 16-bit stereo.\nReason: %s\n", SDL_GetError());
        return 1;
    }

    //Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering
    screen = SDL_SetVideoMode(700, 580, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 255, 255));
	SDL_Flip(screen); //double buffering


    //Load the bitmap into a temporary surface, and check for success
    photo =SDL_LoadBMP("image/cadre.bmp");
    if (photo== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    photo1 =SDL_LoadBMP("image/coeur1.bmp");
    if (photo1== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    photo2 =SDL_LoadBMP("image/coeur2.bmp");
    if (photo2== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    photo3 =SDL_LoadBMP("image/coeur3.bmp");
    if (photo3== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    fin = SDL_LoadBMP("image/diplome.bmp");
    if (fin== NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
    image = SDL_LoadBMP("image/terrain.bmp");
    if (image == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    menuSurface = SDL_LoadBMP("image/menu.bmp");
    if(menuSurface == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    gameOverSurface = SDL_LoadBMP("image/game_over.bmp");
    if(gameOverSurface == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    gameHelpSurface = SDL_LoadBMP("image/aide.bmp");
    if(gameOverSurface == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    right = SDL_LoadBMP("image/fille.bmp");
    if (right == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    left = SDL_LoadBMP("image/fille.bmp");
    if (left == NULL)
    {
        printf("Unable to load bitmap: %s\ n", SDL_GetError());
        return 1;
    }

    up = SDL_LoadBMP("image/fille.bmp");
    if (up == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    down = SDL_LoadBMP("image/fille.bmp");
    if (down == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    SDL_SetColorKey( photo1, SDL_SRCCOLORKEY, SDL_MapRGB(photo1->format, 255, 255, 255) );
    SDL_SetColorKey( photo2, SDL_SRCCOLORKEY, SDL_MapRGB(photo2->format, 255, 255, 255) );
    SDL_SetColorKey( photo3, SDL_SRCCOLORKEY, SDL_MapRGB(photo3->format, 255, 255, 255) );
    SDL_SetColorKey( fin, SDL_SRCCOLORKEY, SDL_MapRGB(fin->format, 255, 255, 255) );

    cadre.x = 580;
    cadre.y = 0;
    cadre.w = photo->w;
    cadre.h = photo->h;

    fille.x = INIT_X_POSITION;
    fille.y = INIT_Y_POSITION;
    fille.w = right->w;
    fille.h = right->h;


    diplome.x=530;
    diplome.y=550;
    diplome.w=fin->w;
    diplome.h=fin->h;


    coeur1.x=600;
    coeur1.y=50;
    coeur1.w=photo1->w;
    coeur1.h=photo1->h;


    coeur2.x=600;
    coeur2.y=120;
    coeur2.w=photo2->w;
    coeur2.h=photo2->h;

    coeur3.x=600;
    coeur3.y=190;
    coeur3.w=photo3->w;
    coeur3.h=photo3->h;

    music = Mix_LoadMUS("music/test.ogg");
    Mix_PlayMusic(music, -1);
    while(done)
    {

        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
                case SDL_QUIT:
                    done = 0;
                break;

                // check for keypresses
                case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = 0;
                    if(event.key.keysym.sym == SDLK_RIGHT)
                    {
                        if((fille.x + fille.w) < image->w) //limite droite
                            fille.x +=5;
                        direction = 0;
                    }

                    if(event.key.keysym.sym == SDLK_LEFT)
                    {
                        if((fille.x) > 0) //limite gauche
                            fille.x -=5;
                        direction = 1;
                    }

                    if(event.key.keysym.sym == SDLK_UP)
                    {
                        if((fille.y) > 0) //limite UP
                            fille.y -=5;
                        direction = 2;
                    }

                    if(event.key.keysym.sym == SDLK_DOWN)
                    {
                        if((fille.y + fille.h) < image->h) //limite DOWN
                            fille.y +=5;
                        direction = 3;
                    }
                    if(event.key.keysym.sym == SDLK_F1)
                    {
                        GAME_STARTED = 1;
                    }

                    if(event.key.keysym.sym == SDLK_h)
                    {
                        if(event.key.state == SDL_PRESSED)
                        {
                            if(GAME_STARTED == 0)
                            {
                                GAME_STARTED = 2;
                            }
                            else if(GAME_STARTED == 2)
                            {
                                GAME_STARTED = 0;
                            }
                        }
                    }

                    if(event.key.keysym.sym == SDLK_s)
                    {
                        if(event.key.state == SDL_PRESSED)
                        {
                            if(ACTIVATE_SOUND == 0)
                            {
                                ACTIVATE_SOUND = 1;
                                Mix_PlayMusic(music, -1);
                            }
                            else
                            {
                                ACTIVATE_SOUND = 0;
                                Mix_HaltMusic();
                            }
                        }
                    }
                    if(hitDetection(direction, fille, screen))
                    {
                        if(direction == 0)
                        {
                            fille.x -= 5;
                        }
                        if(direction == 1)
                        {
                            fille.x += 5;
                        }
                        if(direction == 2)
                        {
                            fille.y += 5;
                        }
                        if(direction == 3)
                        {
                            fille.y -= 5;
                        }

                        GAME_LIFES --;
                        if(GAME_LIFES == 0)
                        {
                            SDL_BlitSurface(gameOverSurface, NULL, screen, NULL);
                            SDL_Flip(screen);
                            SDL_Delay(3000);
                            GAME_STARTED = 0;
                            GAME_LIFES = 3;
                            fille.x = INIT_X_POSITION;
                            fille.y = INIT_Y_POSITION;
                        }
                    }

                    break;
                }
                case SDL_MOUSEBUTTONDOWN:
                {
                    if(event.button.button == SDL_BUTTON_LEFT)
                    {
                        int pixelColor = getPixelColor(screen, event.button.x, event.button.y);
                        printf("LAB Color = %i\n", pixelColor);
                    }
                    break;
                }

            } // end switch
        } // end of message processing



        if(GAME_STARTED == 0)
        {
            SDL_BlitSurface(menuSurface, NULL, screen, NULL);
        }
        else if(GAME_STARTED == 2)
        {
            SDL_BlitSurface(gameHelpSurface, NULL, screen, NULL);
        }
        else
        {

            //Blit the image to the backbuffer
            SDL_BlitSurface(image, NULL, screen, NULL);
            SDL_BlitSurface(photo, NULL, screen, &cadre);
            SDL_BlitSurface(fin, NULL, screen, &diplome);
            if(GAME_LIFES == 3)
            {
                SDL_BlitSurface(photo1, NULL, screen, &coeur1);
                SDL_BlitSurface(photo2, NULL, screen, &coeur2);
                SDL_BlitSurface(photo3, NULL, screen, &coeur3);
            }
            else if(GAME_LIFES == 2)
            {
                SDL_BlitSurface(photo1, NULL, screen, &coeur1);
                SDL_BlitSurface(photo2, NULL, screen, &coeur2);
            }
            else if(GAME_LIFES == 1)
            {
                SDL_BlitSurface(photo3, NULL, screen, &coeur1);
            }


            //Blit the fille to screen
            if(direction == 0)
                SDL_BlitSurface(right,NULL,screen,&fille);
            if(direction == 1)
                SDL_BlitSurface(left,NULL,screen,&fille);
            if(direction == 2)
                SDL_BlitSurface(up,NULL,screen,&fille);
            if(direction == 3)
                SDL_BlitSurface(down,NULL,screen,&fille);
        }


        //clean screen &&
        //Flip the backbuffer to the primary Hardware Video Memory
        SDL_Flip(screen);

    }

    //Release the surface
    SDL_FreeSurface(image);

    //Return success!
    return 0;
}
